/*
 * @Description: 
 * @Autor: fage
 * @Date: 2022-05-11 17:32:45
 * @LastEditors: fage
 * @LastEditTime: 2022-09-19 14:21:55
 */
const { Keyring } = require("@polkadot/api");

class MyKeyring extends Keyring {
  constructor() {
    super({ type: "sr25519", ss58Format: 42 });
  }
  async getPublicKeyFromAccountId(accountId) {
    const pair = this.addFromAddress(accountId);
    return "0x" + this.toHexString(pair.publicKey);
  }
  toHexString(arr) {
    return Array.from(arr, (i) => i.toString(16).padStart(2, "0")).join("");
  }
}


const keyring = new MyKeyring();


module.exports=async function(req,res,next){
    const addr=req.body.addr;
    console.log(addr);
    if(!addr){
        return res.json({msg:'address is null'});
    }
    let publicKey=await keyring.getPublicKeyFromAccountId(addr);
    res.json({msg:'ok',data:publicKey});
}


// keyring.getPublicKeyFromAccountId('cXh5StobuVP4B7mGH9xn8dSsDtXks4qLAou8ZdkZ6DbB6zzxe').then(console.log,console.log);